//
//  ZZMyViewController.m
//  ZuZuBao
//
//  Created by Felix on 2024/4/8.
//

#import "ZZMyViewController.h"
#import "ZZMyHeaderView.h"
#import "ZZMyTableViewCell.h"
#import "ZZMyInfoViewController.h"
#import "ZZOrderViewController.h"
#import "ZZRentExplainViewController.h"
#import "ZZContactUsView.h"
#import "ZZLogoutView.h"
#import "ZZLoginViewController.h"

@interface ZZMyViewController ()<UITableViewDelegate, UITableViewDataSource, ZZMyHeaderViewDelegate, ZZLogoutViewDelegate>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, strong) ZZMyHeaderView * headView;

@end

@implementation ZZMyViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArray = [NSMutableArray arrayWithObjects:@"租用说明", @"联系我们", @"注销账号", @"版本号", nil];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kBackColor;
    self.title = @"我的";
    [self createView];
    [self getData];
}

-(void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight) style:UITableViewStylePlain];
    self.tableView.backgroundColor = kBackColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    
    self.headView = [[ZZMyHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 295)];
    self.headView.delegate = self;
    self.tableView.tableHeaderView = self.headView;
}

-(void)getData {
    [self.headView setViewData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 返回你想要的行数
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 58;
}
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 注册自定义cell的类，如果还没有注册过
    static NSString *cellIdentifier = @"ZZMyTableViewCell";
    [tableView registerClass:[ZZMyTableViewCell class] forCellReuseIdentifier:cellIdentifier];
      
    // 从队列中复用cell或创建新的cell
    ZZMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    // 设置cell的数据
    [cell setCellData:self.dataArray[indexPath.row]];
    if (indexPath.row == 3) {
        cell.contLabel.hidden = NO;
        cell.imgView.hidden = YES;
        cell.contLabel.text = @"v1.0.1";
    } else {
        cell.contLabel.hidden = YES;
        cell.imgView.hidden = NO;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ZZRentExplainViewController * vc = [[ZZRentExplainViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 1) {
        ZZContactUsView * contactView = [[ZZContactUsView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        contactView.phoneLabel.text = @"027-85422572";
        [self.tabBarController.view addSubview:contactView];
    } else if (indexPath.row == 2) {
        ZZLogoutView * logoutView = [[ZZLogoutView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        logoutView.delegate = self;
        [self.tabBarController.view addSubview:logoutView];
    }
}

- (void)clickToMyInfo {
    ZZMyInfoViewController * vc = [[ZZMyInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickToLogin {
    ZZLoginViewController * vc = [[ZZLoginViewController alloc] init];
    vc.block = ^{
        [self.headView setViewData];
        [self.tableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickToItem:(NSInteger)index {
    if (kUid.length == 0) {
        ZZLoginViewController * vc = [[ZZLoginViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    ZZOrderViewController * vc = [[ZZOrderViewController alloc] init];
    vc.currentIndex = index;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickToLogout {
    if (kUid.length == 0) {
        ZZLoginViewController * vc = [[ZZLoginViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    NSString *urlString = [NSString stringWithFormat:@"%@%@", kBaseUrl, KLogOff];
    NSDictionary * headerDic = [NSDictionary dictionaryWithObjectsAndKeys:kToken, @"Authorization", nil];
    
    [kFFNetManager POST:urlString parameters:nil headers:headerDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject:~~~%@", responseObject);
        if ([responseObject[@"code"] integerValue] == 200) {
            kUid = @"";
            kNickName = @"";
            kHeadImg = @"";
            kToken = @"";
            kPhone = @"";
            [self.headView setViewData];
            [self.tableView reloadData];
            [MBProgressHUD showMessage:@"账号已注销" toView:self.view];
        }else {
            [MBProgressHUD showMessage:responseObject[@"message"] toView:self.view];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}


@end
